package db

import (
	"errors"
	"hr/api/dto"

	"gorm.io/gorm"
)

type TabsMenage struct {
	AppModel
	Title    string `gorm:"size:255" json:"title"`
	Icon     string `gorm:"size:255" json:"icon"`
	Link     string `gorm:"size:255" json:"link"`
	CreateBy string `gorm:"size:128" json:"createBy"` // 创建人
	UpdateBy string `gorm:"size:128" json:"updateBy"` // 更新人
	Remark   string `gorm:"size:255" json:"remark"`   //备注
	Status   string `gorm:"size:64;" json:"status"`
	Sort     int    `json:"sort"`
}

func (m *TabsMenage) TableName() string {
	return "tabs"
}

type TabsPageModel struct {
	Total    int64        `json:"total"`
	Result   []TabsMenage `json:"result"`
	PageNo   int          `json:"pageNo"`
	PageSize int          `json:"pageSize"`
}

func GetTabsPaging(pagination dto.Pagination, name string) (TabsPageModel, error) {
	result := TabsPageModel{
		Total:    0,
		Result:   make([]TabsMenage, 0),
		PageNo:   pagination.PageNo,
		PageSize: pagination.PageSize,
	}
	offset := (pagination.PageNo - 1) * pagination.PageSize
	dbs := GetInstance().Model(&TabsMenage{})
	if name != "" {
		dbs = dbs.Where("name LIKE ?", "%"+name+"%")
	}

	dbs = dbs.Order("sort desc")

	if err := dbs.Offset(offset).Limit(pagination.PageSize).Find(&result.Result).Error; err != nil {
		return result, err
	}
	if err := dbs.Offset(-1).Limit(-1).Count(&result.Total).Error; err != nil {
		return result, err
	}
	return result, nil
}

// GetTabs 根据条件查找
func GetTabs(p map[string]interface{}) (TabsMenage, error, string) {
	f := TabsMenage{}
	err := GetInstance().Where(p).First(&f).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return f, errors.New("查无记录"), "0"
		}
		return f, errors.New(err.Error()), "1"
	}
	return f, nil, "2"
}

func CreateTabs(p dto.TabsRequest, user string) (TabsMenage, error) {
	db := GetInstance()
	var tab TabsMenage
	db.Order("sort desc").First(&tab)
	max := 1
	if tab.ID > 0 {
		max = tab.Sort + 1
	}
	d := TabsMenage{
		Title:    p.Title,
		Icon:     p.Icon,
		Link:     p.Link,
		CreateBy: user,
		Remark:   p.Remark,
		Status:   p.Status,
		Sort:     max,
	}
	if err := db.Create(&d).Error; err != nil {
		return d, err
	}
	return d, nil
}

func UpdateTabs(id uint, p dto.TabsRequest, userId string) (TabsMenage, error) {
	f := TabsMenage{
		AppModel: AppModel{
			ID: id,
		},
	}
	err := GetInstance().Model(&f).Updates(TabsMenage{
		Title:    p.Title,
		Icon:     p.Icon,
		Link:     p.Link,
		UpdateBy: userId,
		Remark:   p.Remark,
		Status:   p.Status,
	}).Updates(map[string]interface{}{
		"id": id,
	}).Scan(&f).Error
	if err != nil {
		return f, err
	}
	return f, nil
}

func DeleteTabs(id uint) (TabsMenage, error) {
	f := TabsMenage{}
	err := GetInstance().Unscoped().Where("id = ?", id).Delete(&f).Error
	if err != nil {
		return f, err
	}
	return f, nil
}

func UpTabs(id uint) {

	db := GetInstance()
	var tab TabsMenage
	db.Where("id=?", id).First(&tab)
	if tab.ID == 0 {
		return
	}
	var up TabsMenage
	db.Where("sort > ?", tab.Sort).Order("sort").First(&up)
	if up.ID == 0 {
		return
	}
	//交换sort
	db.Table(tab.TableName()).Where("id=?", id).Update("sort", up.Sort)
	db.Table(tab.TableName()).Where("id=?", up.ID).Update("sort", tab.Sort)

}

func DownTabs(id uint) {
	db := GetInstance()
	var tab TabsMenage
	db.Where("id=?", id).First(&tab)
	if tab.ID == 0 {
		return
	}
	var down TabsMenage
	db.Where("sort < ?", tab.Sort).Order("sort desc").First(&down)
	if down.ID == 0 {
		return
	}
	//交换sort
	db.Table(tab.TableName()).Where("id=?", id).Update("sort", down.Sort)
	db.Table(tab.TableName()).Where("id=?", down.ID).Update("sort", tab.Sort)
}
